#include "LibrarySystem.h"
#include <iostream>
#include <cstdlib>
using namespace std;

void displayMenu() {
    cout << "========== 图书管理系统 ==========" << endl;
    cout << "1. 图书管理" << endl;
    cout << "2. 读者管理" << endl;
    cout << "3. 借阅管理" << endl;
    cout << "4. 查询功能" << endl;
    cout << "0. 退出系统" << endl;
    cout << "请选择操作: ";
}

void bookManagementMenu(LibrarySystem& system) {
    int choice;
    do {
        cout << "\n========== 图书管理 ==========" << endl;
        cout << "1. 添加图书" << endl;
        cout << "2. 删除图书" << endl;
        cout << "3. 修改图书" << endl;
        cout << "4. 查询图书" << endl;
        cout << "5. 显示所有图书" << endl;
        cout << "0. 返回主菜单" << endl;
        cout << "请选择操作: ";
        cin >> choice;

        switch (choice) {
        case 1: system.addBook(); break;
        case 2: system.deleteBook(); break;
        case 3: system.modifyBook(); break;
        case 4: system.searchBooks(); break;
        case 5: system.displayAllBooks(); break;
        case 0: cout << "返回主菜单..." << endl; break;
        default: cout << "无效选择！" << endl;
        }
    } while (choice != 0);
}

void readerManagementMenu(LibrarySystem& system) {
    int choice;
    do {
        cout << "\n========== 读者管理 ==========" << endl;
        cout << "1. 添加读者" << endl;
        cout << "2. 删除读者" << endl;
        cout << "3. 修改读者" << endl;
        cout << "4. 查询读者" << endl;
        cout << "5. 黑名单管理" << endl;
        cout << "6. 显示所有读者" << endl;
        cout << "0. 返回主菜单" << endl;
        cout << "请选择操作: ";
        cin >> choice;

        switch (choice) {
        case 1: system.addReader(); break;
        case 2: system.deleteReader(); break;
        case 3: system.modifyReader(); break;
        case 4: system.searchReaders(); break;
        case 5: system.manageBlacklist(); break;
        case 6: system.displayAllReaders(); break;
        case 0: cout << "返回主菜单..." << endl; break;
        default: cout << "无效选择！" << endl;
        }
    } while (choice != 0);
}

void borrowManagementMenu(LibrarySystem& system) {
    int choice;
    do {
        cout << "\n========== 借阅管理 ==========" << endl;
        cout << "1. 借阅图书" << endl;
        cout << "2. 归还图书" << endl;
        cout << "3. 显示所有借阅记录" << endl;
        cout << "4. 显示逾期记录" << endl;
        cout << "0. 返回主菜单" << endl;
        cout << "请选择操作: ";
        cin >> choice;

        switch (choice) {
        case 1: system.borrowBook(); break;
        case 2: system.returnBook(); break;
        case 3: system.displayAllRecords(); break;
        case 4: system.displayOverdueRecords(); break;
        case 0: cout << "返回主菜单..." << endl; break;
        default: cout << "无效选择！" << endl;
        }
    } while (choice != 0);
}

void queryMenu(LibrarySystem& system) {
    int choice;
    do {
        cout << "\n========== 查询功能 ==========" << endl;
        cout << "1. 图书查询" << endl;
        cout << "2. 读者查询" << endl;
        cout << "3. 读者借阅记录查询" << endl;
        cout << "0. 返回主菜单" << endl;
        cout << "请选择操作: ";
        cin >> choice;

        switch (choice) {
        case 1: system.bookQuery(); break;
        case 2: system.readerQuery(); break;
        case 3: system.readerRecordQuery(); break;
        case 0: cout << "返回主菜单..." << endl; break;
        default: cout << "无效选择！" << endl;
        }
    } while (choice != 0);
}

int main() {
    LibrarySystem system;
    int choice;

    cout << "欢迎使用图书管理系统！" << endl;

    do {
        displayMenu();
        cin >> choice;

        switch (choice) {
        case 1: bookManagementMenu(system); break;
        case 2: readerManagementMenu(system); break;
        case 3: borrowManagementMenu(system); break;
        case 4: queryMenu(system); break;
        case 0:
            system.saveAllData();
            cout << "感谢使用，再见！" << endl;
            break;
        default: cout << "无效选择，请重新输入！" << endl;
        }
    } while (choice != 0);

    return 0;
}